package com.xiaoxi.open.design.behavioral.chainofresponsibilitypattern.service.password;

import com.xiaoxi.open.design.behavioral.chainofresponsibilitypattern.domain.password.CheckResult;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName KeyBoardCheck
 * @Description
 * @Author xiaoxi
 * @Date 2022/7/11 21:40
 * @Version 1.0
 */
@Component
public class KeyBoardCheck extends AbstractCheckPassword {

    public static String[][] keyCode = new String[][]{{"`~·", "1！!", "2@@", "3#", "4$￥", "5%", "6^……", "7&", "8*", "9(（", "0）)", "-_", "=+"}, {" ", "qQ", "wW", "eE", "rR", "tT", "yY", "uU", "iI", "oO", "pP", "[{【", "]}】", "\\|、"}, {" ", "aA", "sS", "dD", "fF", "gG", "hH", "jJ", "kK", "lL", ";:", "'\"’“"}, {" ", "zZ", "xX", "cC", "vV", "bB", "nN", "mM", ",《<", ".>》", "/?？"}};

    @Override
    public void doHandler(String password, CheckResult checkResult) throws Exception {
        char[] c = password.toCharArray();
        List<Integer> x = new ArrayList<>();
        List<Integer> y = new ArrayList<>();

        label97:
        for (char temp : c) {
            for (int j = 0; j < keyCode.length; ++j) {
                for (int k = 0; k < keyCode[j].length; ++k) {
                    String jk = keyCode[j][k];
                    if (jk.contains(String.valueOf(temp))) {
                        x.add(j);
                        y.add(k);
                        continue label97;
                    }
                }
            }
        }

        boolean flag = false;

        for (int i = 0; i < x.size() - 3; ++i) {
            if (x.get(i).equals(x.get(i + 1)) && x.get(i + 1).equals(x.get(i + 2)) && x.get(i + 2).equals(x.get(i + 3))) {
                if (y.get(i) > y.get(i + 3)) {
                    if (y.get(i) - 1 == y.get(i + 1) && y.get(i) - 2 == y.get(i + 2) && y.get(i) - 3 == y.get(i + 3)) {
                        flag = true;
                        break;
                    }
                } else if (y.get(i) + 1 == y.get(i + 1) && y.get(i) + 2 == y.get(i + 2) && y.get(i) + 3 == y.get(i + 3)) {
                    flag = true;
                    break;
                }
            } else if (!(x.get(i)).equals(x.get(i + 1)) && !(x.get(i + 1)).equals(x.get(i + 2)) && !(x.get(i)).equals(x.get(i + 2)) && !(x.get(i)).equals(x.get(i + 3)) && !(x.get(i + 1)).equals(x.get(i + 3)) && !(x.get(i + 2)).equals(x.get(i + 3))) {
                if ((y.get(i)).equals(y.get(i + 1)) && (y.get(i + 1)).equals(y.get(i + 2)) && (y.get(i + 2)).equals(y.get(i + 3))) {
                    flag = true;
                    break;
                }

                if ((y.get(i)).equals(y.get(i + 1) + 1) && (y.get(i + 1)).equals(y.get(i + 2) + 1) && (y.get(i + 2)).equals(y.get(i + 3) + 1)) {
                    flag = true;
                    break;
                }
            }
        }

        if (flag) {
            checkResult.getErrorReason().add("密码不应包含键盘上横向或者纵向任意连续的四个字符或shift转换字符");
        }
    }
}
